package in.muton.edushare.pages.coursepages;

import in.muton.edushare.common.UserFilter;
import in.muton.edushare.processing.objects.CourseDataHandler;
import in.muton.edushare.wicket.models.CayenneList;
import in.muton.edushare.wicket.models.InnerList;
import in.muton.panels.paginator.EntityListing;
import in.muton.panels.paginator.badgehelpers.user.CourseBadgeRoles;

import java.util.List;

import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.PropertyModel;

import com.edushare.common.constants.ApplicationConstants;
import com.edushare.common.vo.Users;

public class CoursePeople extends CourseBase {

	public CoursePeople(final PageParameters params) {
		super(params);
		activePage = params.getString("3", "courseEveryone");
		CourseBadgeRoles cbr = new CourseBadgeRoles(getCourseInstance());
		add(new EntityListing<Users>("thumbList", new InnerList<Users>() {
			private static final long serialVersionUID = 8168468769304968484L;

			@Override
			public List<IModel<Users>> getObject() {
				return addPeople(params).getObject();
			}
		}, cbr, this));
		addPageTitle();
		Form<Void> f = new Form<Void>("filterForm");
		add(f);
		f.add(new TextField<String>("filterString", new PropertyModel<String>(
				this, "filterString")));

	}

	private CayenneList<Users> addPeople(PageParameters params) {
		CayenneList<Users> cL = new CayenneList<Users>(null);
		CourseDataHandler cd = new CourseDataHandler(getCourseInstance());
		if (activePage.compareTo("courseEveryone") == 0) {
			cL = cd.getPeople();
		} else if (activePage.compareTo("courseTeachers") == 0) {
			cL = cd.getTeachers();
		} else if (activePage.compareTo("courseStudents") == 0) {
			cL = cd.getStudents();
		}
		return cL = UserFilter.filterByString(cL, filterString);
	}

	@Override
	protected String getActivePage(PageParameters params) {
		return params.getString("3", "courseEveryone");
	}

	@Override
	public String getPageTitle() {
		return getLoggedInUser().getCompleteUserName() + " | "
				+ getCourse().getObject().getCourseName() + " | People | "
				+ ApplicationConstants.APPLICATION_NAME;
	}

	private String filterString = "";

	public String getFilterString() {
		return filterString;
	}

	public void setFilterString(String filterString) {
		this.filterString = filterString;
	}

	private String activePage = "";

	public void addPageTitle() {
		if (activePage.compareTo("courseEveryone") == 0) {
			add(new Label("peopleLabel", "All"));
		} else if (activePage.compareTo("courseTeachers") == 0) {
			add(new Label("peopleLabel", "Faculty"));
		} else if (activePage.compareTo("courseStudents") == 0) {
			add(new Label("peopleLabel", "Students"));
		}
	}
}
